package com.oromwin.repos.server.response.media;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.FileTable;
import com.oromwin.repos.server.response.NormalResponse;

public class ListMediaResponse extends NormalResponse {
    private Log log = Logger.getLog(ListMediaResponse.class);
    private List<AbstractReposTable> ReposTables = null;
    
    public ListMediaResponse(boolean result, String description, List<AbstractReposTable> reposTables) {
        Result = result;
        Description = description;
        ReposTables = reposTables;
    }
    
    public void send(DataOutputStream dos) throws ReposException {
        try {
            dos.writeBoolean(Result);
            IOHelper.writeUTF8(dos, Description.toString());
            dos.writeInt(ReposTables.size());
            for (AbstractReposTable abstractTable : ReposTables) {
                if (abstractTable instanceof FileTable) {
                    FileTable reposTable = (FileTable) abstractTable;
                    IOHelper.writeUTF8(dos, reposTable.getVolumeId());
                    IOHelper.writeUTF8(dos, reposTable.getMediaId());
                    IOHelper.writeUTF8(dos, reposTable.getFileId());
                    IOHelper.writeUTF8(dos, reposTable.getFileName());
                    IOHelper.writeUTF8(dos, reposTable.getFileFormat());
                    IOHelper.writeUTF8(dos, reposTable.getFilePath().toString());
                    dos.writeLong(reposTable.getFileBytes());
                    IOHelper.writeUTF8(dos, reposTable.getFileCategory());
                    dos.writeBoolean(reposTable.getFileStatus());
                    dos.writeBoolean(reposTable.getFilePdfConvFlag());
                    dos.writeBoolean(reposTable.getFileCompressionFlag());
                    IOHelper.writeUTF8(dos, reposTable.getFileCreateDate());
                    IOHelper.writeUTF8(dos, reposTable.getFileUpdateDate());
                } else {
                    throw ReposException.create(log, String.format("Illegal AbstractTable type, {%s}", abstractTable.getClass().toString()));
                }
            }
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }
}
